package com.gbr.client.view;

import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.stage.WindowEvent;

import com.gbr.client.IJavaFxReady;
import com.gbr.client.presentation.IDesktopPresentation;
import com.ng.framework.AbstractPlugin;

public class DesktopComponent extends AbstractPlugin {
    private IDesktopPresentation presentation;
    private DesktopGui          desktopGui;

    @Override
    protected void activateComponent() {
        Platform.runLater(new Runnable() {
            public void run() {
                desktopGui = new DesktopGui(presentation);
                desktopGui.setOnCloseRequest(new CloseHandler());
                desktopGui.show();
            }
        });
    }

    @Override
    protected void deactivateComponent() {
        System.out.println("*** Deactivating: "+this.getClass());
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                desktopGui.close();
                System.out.println("*** Gui closed");
            }
        });
    }

    public final void bindPresentation(IDesktopPresentation service) {
        presentation = service;
    }

    public final void unbindPresentation(IDesktopPresentation service) {
        presentation = null;
    }

    public final void bindFxReady(IJavaFxReady service) {
        // used to notify when javafx is ready
    }

    class CloseHandler implements EventHandler<WindowEvent> {
        @Override
        public void handle(WindowEvent event) {
            presentation.shutdown();
        }
    }
}
